//
//  SGDefaultItemView.m
//  SpringGraph Test
//
//  Created by Jack Herrington on 5/31/10.
//  Copyright 2010 Myself. All rights reserved.
//

#import "SGExampleNode.h"


@implementation SGExampleNode

@synthesize text, font, textColor, boxColor, cornerRadius;

- (SGExampleNode *)initWithText:(NSString *)_text {
	self = [super initWithFrame:CGRectMake( 0, 0, 100, 50 )];
	if ( self ) {
		self.text = _text;
	}
	return self;
}

- (void)dealloc {
	[_textLabel release];
	[super dealloc];
}

- (UILabel *)textLabel {
	return _textLabel;
}

- (void)didMoveToSuperview {
	self.font = [UIFont systemFontOfSize:24];
	self.textColor = [UIColor blackColor];
	self.userInteractionEnabled = YES;
	
	_textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
	_textLabel.font = self.font;
	_textLabel.textColor = self.textColor;
	_textLabel.text = self.text;
	_textLabel.opaque = NO;
	_textLabel.backgroundColor = [UIColor colorWithWhite:0 alpha:0.0];
	_textLabel.clearsContextBeforeDrawing = NO;
	_textLabel.textAlignment = UITextAlignmentCenter;
	[self addSubview:_textLabel];
	
	self.clipsToBounds = NO;
	self.cornerRadius = 10;
	self.boxColor = [UIColor colorWithWhite:0.8 alpha:1];

	self.opaque = NO;
	self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.0];
	self.clearsContextBeforeDrawing = NO;
	self.contentMode = UIViewContentModeRedraw;
}

- (void)drawBackground:(CGContextRef)context {
    CGRect rrect = self.bounds;
    
    CGFloat radius = self.cornerRadius;
    CGFloat width = CGRectGetWidth(rrect);
    CGFloat height = CGRectGetHeight(rrect);
    
    if (radius > width/2.0)
        radius = width/2.0;
    if (radius > height/2.0)
        radius = height/2.0;    
    
    CGFloat minx = CGRectGetMinX(rrect);
    CGFloat midx = CGRectGetMidX(rrect);
    CGFloat maxx = CGRectGetMaxX(rrect);
    CGFloat miny = CGRectGetMinY(rrect);
    CGFloat midy = CGRectGetMidY(rrect);
    CGFloat maxy = CGRectGetMaxY(rrect);
    CGContextMoveToPoint(context, minx, midy);
    CGContextAddArcToPoint(context, minx, miny, midx, miny, radius);
    CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius);
    CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
    CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius);
    CGContextClosePath(context);

	[self.boxColor setFill];
    CGContextDrawPath(context, kCGPathFill);
}

- (void)drawRect:(CGRect)rect {
	CGContextRef context = UIGraphicsGetCurrentContext();
	[self drawBackground:context];
}

@end
